package com.cls.business.vo;

import com.cls.business.entity.ApplicantInfo;
import com.cls.business.entity.DeclareTopic;
import com.cls.business.entity.MemberInfo;
import com.cls.common.enums.DictEnum;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.wuwenze.poi.annotation.Excel;
import com.wuwenze.poi.annotation.ExcelField;
import lombok.Data;

import java.util.List;

/**
 * 课题申报汇总表-实体
 */
@Data
@Excel("课题申报汇总表")
public class SummaryDeclareTopic {
    // 编号
    @ExcelField(value = "编号")
    private String code;
    // 课题名称
    @ExcelField(value = "课题名称")
    private String name;
    // 主持人
    @ExcelField(value = "主持人")
    private String leaderName;
    // 所在单位
    @ExcelField(value = "所在单位")
    private String company;
    // 职务职称
    @ExcelField(value = "职务职称")
    private String titleType;

    private String companyAndTitleType;
    // 电话
    @ExcelField(value = "电话")
    private String mobile;
    // 邮箱
    @ExcelField(value = "邮箱")
    private String email;
    // 课题组成员
    @ExcelField(value = "课题组成员")
    private String memberNames;
    // 学科
    @ExcelField(value = "学科")
    private String subjectType;
    // 成功形式
    @ExcelField(value = "成功形式")
    private String achievementFormType;
    // 字数
    @ExcelField(value = "字数")
    private String achievementFormSize;
    // 资格审查结果
    @ExcelField(value = "资格审查结果")
    private String statusType;
    // 原因
    @ExcelField(value = "原因")
    private String checkResult;

    // 主要观点
    private String mainPoint;

    // 备注
    private String note = "";

    // 立项编号
    private String itemCode = "";

    // 课题组织单位
    private String topicUnitName = "";

    // 牵头人
    private String initiator = "";

    // 级别
    private String levelType= "";

    // 级别
    private String topicCode= "";

    private ISysDictService dictService;
    public SummaryDeclareTopic(){

    }

    public SummaryDeclareTopic(ISysDictService dictService, DeclareTopic declareTopic, ApplicantInfo applicantInfo, List<MemberInfo> memberInfos){
        if(declareTopic == null || applicantInfo == null){
            throw new IllegalArgumentException("参数异常！");
        }
        this.dictService = dictService;
        this.code = declareTopic.getCode();
        this.name = declareTopic.getName();
        this.leaderName = declareTopic.getLeaderName();
        this.subjectType = convertCodeToName(declareTopic.getSubjectTypeCode());
        this.achievementFormType = convertCodesToNames(declareTopic.getAchievementFormTypeCode());
        this.achievementFormSize = declareTopic.getAchievementFormSize() + "万";
        this.checkResult = declareTopic.getCheckResult();
        this.company = applicantInfo.getCompany();
        this.titleType =applicantInfo.getTitleTypeCode();
        //this.titleType =convertCodeToName(applicantInfo.getPositionTypeCode()) + " " + convertCodeToName(applicantInfo.getTitleTypeCode());
        this.companyAndTitleType = applicantInfo.getCompany() + "/" + applicantInfo.getProfessionCode();
        this.mobile = applicantInfo.getMobile();
        this.email = applicantInfo.getEmail();
        this.mainPoint = declareTopic.getMainPoint();
        this.levelType = convertCodeToName(declareTopic.getLevelTypeCode());
        if(memberInfos != null){
            for(int i = 0; i < memberInfos.size(); i++){
                MemberInfo memberInfo = memberInfos.get(i);
                if(i == 0){
                    memberNames = memberInfo.getName();
                } else {
                    memberNames += "、" + memberInfo.getName();
                }
            }
        }
        this.statusType = getStatusType(declareTopic.getStatusTypeCode());
        this.itemCode = declareTopic.getItemCode();
        this.topicCode = declareTopic.getTopicCode();
    }

    private String convertCodeToName(String code) {
        SysDict dict = dictService.findByCode(code);
        if(dict == null){
            return "";
        }
        return dict.getName();
    }

    private String convertCodesToNames(String codes) {
        if(codes == null){
            return "";
        }
        StringBuilder builder = new StringBuilder();
        String[] groupCode = codes.split(",");
        for(String one:groupCode){
            builder.append(convertCodeToName(one) + " ");
        }
        return builder.toString();
    }

    private String getStatusType(String statusTypeCode){
        String statusType = "合格";
        if(DictEnum.CHECK_FAILED.getCode().equals(statusTypeCode)){
            statusType = "不合格";
        } else if(DictEnum.NO_APPLY.getCode().equals(statusTypeCode) ||
                DictEnum.PRE_APPLY.getCode().equals(statusTypeCode)){
            statusType = "未提交";
        } else if(DictEnum.APPLY.getCode().equals(statusTypeCode)){
            statusType = "未审核";
        }
        return statusType;
    }
}
